<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="660px" 
     @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :inline="true" :model="routerModel" :rules="formRules" ref="routerForm" 
      label-width="80px" label-position="right" size="small">
      <el-row>
        <el-col :span="8">
          <el-form-item prop="filenumber" label="档案号">
            <el-input  :maxlength="50" v-model="routerModel.filenumber" autofocus></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="name" label="姓名">
            <el-input :maxlength="50" v-model="routerModel.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="valuationdate" label="评估日期">
            <el-input :maxlength="50" v-model="routerModel.valuationdate"></el-input>
          </el-form-item>          
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="2">
          <el-form-item prop="somatization" label="躯体化">
              <el-input :maxlength="50" v-model="routerModel.somatization"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item prop="obsessive" label="强迫症化">
             <el-input :maxlength="50" v-model="routerModel.obsessive"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item prop="interpersonal" label="人际关系敏感">
             <el-input :maxlength="50" v-model="routerModel.interpersonal"></el-input>
          </el-form-item>          
        </el-col>
        <el-col :span="2">
          <el-form-item prop="hesitate" label="犹豫">
             <el-input :maxlength="50" v-model="routerModel.hesitate"></el-input>
          </el-form-item>          
        </el-col> 
         <el-col :span="2">
          <el-form-item prop="anxiety" label="焦虑">
            <el-input :maxlength="50" v-model="routerModel.anxiety"></el-input>          
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item prop="hostility" label="敌对">
            <el-input :maxlength="50" v-model="routerModel.hostility"></el-input>          
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item prop="terror" label="恐怖">
            <el-input :maxlength="50" v-model="routerModel.terror"></el-input>          
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item prop="stubborn" label="偏执">
            <el-input :maxlength="50" v-model="routerModel.stubborn"></el-input>          
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item prop="psychotic" label="精神病性">
            <el-input :maxlength="50" v-model="routerModel.psychotic"></el-input>          
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('routerForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveCheckmind,updatemind } from '@/api/checkmind'

export default {
  name: 'checkmind-edit',
  // props: ['parentRouterList'],
  data() {
    return {
      loading: false,
      routerModel: {
        id: 0,
        filenumber: '',
        name: '',
        valuationdate: '',
        somatization:'',
        obsessive:'',
        interpersonal:'',
        hesitate:'',
        anxiety:'',
        hostility:'',
        terror: '',
        stubborn:'',
        psychotic:''
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      formRules: {
        name: [
          { required: true, message: '请输入患者姓名', trigger: 'blur' },
          { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }
        ] 
      },
      // parentRouters: [
      //   {
      //     value: 0,
      //     label: '请选择'
      //   }        
      // ]   
    }
  },
  methods: {
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['routerForm'].clearValidate()
        // this.$refs['routerCode'].focus()
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {          
          if(this.dialogProps.action === 'add') {
            this.doSaveRouter()
          } else {
            this.doUpdateRouter()
          }
        } else {
          return false;
        }
      });
    },    
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    doUpdateRouter() {
      this.loading = true
      updatemind(this.routerModel.id, this.routerModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)      
      })      
    },
    doSaveRouter() {
      this.loading = true
      saveCheckmind(this.routerModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)       
      })      
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })      
    }    
  },
  mounted: function() {
    this.$nextTick(() => {  
      this.$on('openEditmindDialog', function(router) {
        this.routerModel = router
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改心理评估'
        this.dialogProps.visible = true
      })
      this.$on('openAddmindDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '添加心理评估'
        this.routerModel = {}
        this.dialogProps.visible = true
      })
    })
  }    
}
</script>

<style lang="scss" scoped>
  .parent-router-select {
    width: 188px;
  }
  .properties-textarea {
    width: 496px;
  }
</style>


